<?php
 
 class BusesController extends AppController{
    public $uses = array('Station','Tax','Bus');

    public function beforeFilter(){
      parent::beforeFilter();
      $this->Auth->allow('searchBuses', 'searchStation');
    }

    public function isAuthorized($user){
      // if (in_array($this->action, array('edit', 'delete','add','view'))){
      //   // if ($user['role'] != 'Admin'){
      //   //   return false;
      //   // }
      // }
    
      return true;
    }

    public function index() {
    	$this->set('buses', $this->Bus->find('all'));
		   
    }
    public function view($id = null) {
      $this->Bus->id = $id;
      $this->set('bus', $this->Bus->read());
    }
    public function add() {
      $this->set ('taxes', $this->Tax->find('all'));
      $this->set ('stations', $this->Station->find('all', array('conditions'=> array('Station.bus =' => 1))));
      if($this->request->is('post'))
      {
         if($this->Bus->saveAll($this->request->data))
         {
           $this->redirect(array('action' => 'index'));
         } 
      }
    }
    public function edit($id = null) {
      $this->set('stations', $this->Station->find('all', array('conditions'=> array('Station.bus =' => 1))));
      $this->set('taxes', $this->Tax->find('all'));
      $this->Bus->id = $id;

      if ($this->request->is('get')) {
        $this->request->data = $this->Bus->read();
      } 
      else {
        if ($this->Bus->saveAll($this->request->data)) {
            $this->redirect(array('action' => 'index'));
        }
    }
}
   public function delete($id) {
    if (!$this->request->is('post')) {
    }
    if ($this->Bus->delete($id)) {
        $this->redirect(array('action' => 'index'));
    }
  }
  public function searchStation(){
    $this->set ('stations', $this->Station->find('all', array('conditions'=> array('Station.bus =' => 1))));
    $this->set('filters', 'show');
    $this->set('type', 'bus');
    if ($this->request->is('post')) {
      $selectedStation = $_POST['Station'];
      $this->redirect(array('action' => 'searchBuses/'.$selectedStation));
    }
  }

  public function searchBuses($selectedStation){
    $this -> set ('buses', $this->Bus->find('all', array('conditions'=> array('Bus.station_acronym =' => $selectedStation))));
    
  }
     		
 }

?>